import {
	URLs
} from '@/config/index.js'

/**
 * 上传图片
 */
function uploadImage(callback, count = 9) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count,
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], //从相册选择
			success: function(res) {
				const tempFilePath = res.tempFilePaths;
				tempFilePath.forEach((item, index) => {
					uploadFile(tempFilePath[index], callback)
				})
			}
		});
	})
}


/**
 * 上传文件
 */
function uploadFile(tempPath, callback) {
	uni.showLoading({
		title: '上传中'
	})
	uni.uploadFile({
		url: URLs.host + '/sys/common/upload', //上传地址
		filePath: tempPath,
		name: 'file',
		formData: {
			"Content-Type": "multipart/form-data"
		},
		header: {
			// Authorization: store.getters.token
		},
		success: function(res) {
			var data = JSON.parse(res.data);
			uni.hideLoading()
			if(data.code === 401){
				uni.showModal({
					title: data.message,
					success:res => {
						if(res.confirm){
							uni.navigateTo({
								url:'/pages/user/login'
							})
						}
					}
				})
			}else{
				callback(data);
			}
			
		},
		fail: (err) => {
			console.log(err)
		},
		complete: () => {
			console.log(1);
		}
	});
}

export {
	uploadImage
}